import OssClient from 'ali-oss'
import fg from 'fast-glob'
import dotenv from 'dotenv'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

dotenv.config({ path: '.env.oss' })
const dir = resolve(dirname(fileURLToPath(import.meta.url)), '../dist')

const client = new OssClient({
  bucket: process.env.OSS_BUCKET!,
  region: process.env.OSS_REGION!,
  accessKeyId: process.env.OSS_ID!,
  accessKeySecret: process.env.OSS_SECRET!,
})

const listP = () =>
  client
    .listV2({ 'max-keys': '1000' }, {})
    .then(res => res.objects)
    .then(arr => arr.filter(item => !item.name.startsWith('files/')))
    .then(arr => arr.map(item => item.name))
    .then(keys => {
      if (!keys.length) return
      // return client.deleteMulti(keys).then(res => console.log('deleteMulti', res.res.status))
    })
const uploadP = () =>
  fg(dir + '/**')
    // .then(files => {
    //   console.log('upload files', files)
    //   return files
    // })
    .then(files =>
      files.filter(
        f =>
          !f.includes('/assets/cesium-D_l4O9YC.js') &&
          !f.includes('/assets/react-wpXbf5jk.js') &&
          !f.includes('/dist/cesium/')
      )
    )
    .then(files => Promise.all(files.map(f => client.put(f.replace(dir, ''), f))))
uploadP()
